import request, { makePromiseResult } from '@/api'
import { DataModelVersionType, ServiceVersionType } from '@coolphk/shared-types/low-code'
import { mockDataModels } from '@/api/mockData'

const dataModels: DataModelVersionType[] = mockDataModels
export const getDataModelVersionById = (id) => {
  // return request.get<DataModelType>('data-model/' + id)
  // return makePromiseResult(dataModels.find((item) => item.id === id))
  return request.DataModelApi.getDataModelVersion(id)
}
export const getDataModelByServiceVersion = async (serviceVersion: ServiceVersionType) => {
  const dataModel: { inputModel: DataModelVersionType, outputModel: DataModelVersionType } = {
    inputModel: null,
    outputModel: null,
  }

  const promises = [];

  if (serviceVersion.inputModelVersionId) {
    promises.push(getDataModelVersionById(serviceVersion.inputModelVersionId));
  } else {
    promises.push(Promise.resolve(null));
  }

  if (serviceVersion.outputModelVersionId) {
    promises.push(getDataModelVersionById(serviceVersion.outputModelVersionId));
  } else {
    promises.push(Promise.resolve(null));
  }

  const [inputModel, outputModel] = await Promise.all(promises);

  dataModel.inputModel = inputModel;
  dataModel.outputModel = outputModel;

  return dataModel;
}

